<?php
/**
 * \file fleamarket.php
 * \author René H. Thomsen
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * \brief Controller responsible for creating, updating, getting, and deleting flea markets from the database.
 */
class Fleamarket extends CI_Controller
{
	public function Index()
	{
		echo "Nothing to see, move along.";
	}

	/**
	 * \brief Returns all flea markets from the database.
	 */
	public function GetAllFleaMarkets()
	{
		// Get data
		$queryString = 'SELECT Id, MarketName, City, UserName, Description, Address,
		Latitude, Longitude, DayOfWeek, IFNULL(AVG(Rating), 0.0) as AverageRating, Picture,
		StartTime, EndTime, DateForDay
		FROM 
		(SELECT MarketTable.Id, MarketTable.Name as MarketName, City, Users.Name as UserName, EventInfo.Description as Description, Address,
				Latitude, Longitude, DayInfo.DateForDay, (DAYOFWEEK(DayInfo.DateForDay)) as DayOfWeek, Ratings.Rating, Picture, 
				DayInfo.FromTime as StartTime, DayInfo.ToTime as EndTime	
				FROM Markets as MarketTable
								 JOIN Users ON MarketTable.Users_Email = Users.Email
								 JOIN EventInfo ON MarketTable.EventInfo_Id = EventInfo.Id
								 JOIN DayInfo ON EventInfo.Id = DayInfo.EventInfo_Id
								 LEFT JOIN Ratings ON MarketTable.Id = Ratings.Markets_Id
				WHERE DayInfo.DateForDay >= CURRENT_DATE()
				ORDER BY DayInfo.DateForDay ASC
		) as MarketInner
		GROUP BY Id
		ORDER BY DayOfWeek';
		
		$query = $this->db->query($queryString);
		
		// Return data
		/*$fleamarkets = array();
		foreach($query->result() as $row)
		{
			$fleamarkets[] = array('Address' => $row->Address);
		}*/
		$fleamarkets = $query->result();
		$result = $this->json->GetJsonResult(1, 0, 'Got flea markets', $fleamarkets);
		$this->output->set_content_type('application/json');
		$this->output->set_output($result);
		return;
	}

	/**
	 * \brief Gets specific flea market.
	 */
	public function GetFleaMarket()
	{
		// Get data
		$id = $_GET['id'];
		$queryString = 'SELECT Id, MarketName, City, UserName, Description, Address,
		Latitude, Longitude, DayOfWeek, IFNULL(AVG(Rating), 0.0) as AverageRating, Picture,
		StartTime, EndTime, DateForDay
		FROM 
		(SELECT MarketTable.Id, MarketTable.Name as MarketName, City, Users.Name as UserName, EventInfo.Description as Description, Address,
				Latitude, Longitude, DayInfo.DateForDay, (DAYOFWEEK(DayInfo.DateForDay)) as DayOfWeek, Ratings.Rating, Picture, 
				DayInfo.FromTime as StartTime, DayInfo.ToTime as EndTime	
				FROM Markets as MarketTable
								 JOIN Users ON MarketTable.Users_Email = Users.Email
								 JOIN EventInfo ON MarketTable.EventInfo_Id = EventInfo.Id
								 JOIN DayInfo ON EventInfo.Id = DayInfo.EventInfo_Id
								 LEFT JOIN Ratings ON MarketTable.Id = Ratings.Markets_Id
				WHERE DayInfo.DateForDay >= CURRENT_DATE()
				ORDER BY DayInfo.DateForDay ASC
		) as MarketInner
		WHERE Id = '.$id.'
		GROUP BY Id
		ORDER BY DayOfWeek';
		
		$query = $this->db->query($queryString);
		
		$fleamarket = $query->result();
		$result = $this->json->GetJsonResult(1, 0, 'Got flea markett', $fleamarket[0]);
		$this->output->set_content_type('application/json');
		$this->output->set_output($result);
		
		return;
	}

	public function Rate()
	{
		$rating = $this->input->post('rating');
		$market_id = $this->input->post('market_id');

		$return = $this->auth->Auth();
		if($return != FALSE)
		{
			if($this->session->userdata('privilege_id') > 0)
			{
				$this->db->where('Users_Email', $this->session->userdata('email'));
				$this->db->where('Markets_Id', $market_id);
				$this->db->delete('Ratings');


				$data = array(
	   			'Markets_Id' => $market_id,
	   			'Users_Email' => $this->session->userdata('email'),
	   			'Rating' => $rating
				);

				$dbres = $this->db->insert('Ratings', $data);
				
				
				echo $this->json->GetJsonResult(1, 0, "Market Rated", null);
			}
			else
			{
				echo $this->json->GetJsonResult(0, 1, "Error not high enough privileges", null);
			}
		}
		else
		{
			echo $this->json->GetJsonResult(0, 99, "Error not logged in", null);
		}
	}
}

?>